#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define SERVER_IP   "127.0.0.1"
#define PORT        5000
#define BUFSIZE     1024

int main()
{
    int fd;
    char buf[BUFSIZE] = {0};

    struct hostent *he;   //the struct that will get information about remote host
    struct sockaddr_in  server;

    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        printf("create socket error!\n");
        return -1;
    }

    bzero(&server, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);
    //server.sin_addr = *((struct in_addr *)he->h_addr);
    server.sin_addr.s_addr = inet_addr(SERVER_IP);

    // start to conncet 
    if (connect(fd, (struct sockaddr *)&server, sizeof(server)) == -1) {
        printf("connect error\n");
        return -1;
    }

    while (1) {
        memset(buf, 0, sizeof(buf));
        int n = read(0, buf, BUFSIZE);
        // printf("input: bytes=%d, data=%s\n", n, buf);
        write(fd, buf, n);
    }
    close(fd);

    return 0;
}

